package 算法.leetcode.offer.nk;

import 算法.leetcode.offer.base.ListNode;

/**
 * @author lchenglong
 * @date 2022/3/26
 */
public class BM14 {

    public static void main(String[] args) {
        ListNode head = ListNode.init(new int[]{1,4,6,3,7});
        BM14 instance = new BM14();
        ListNode listNode = instance.oddEvenList(head);
        ListNode.print(listNode);

    }
    public ListNode oddEvenList (ListNode head) {
        // write code here
        ListNode o=null,j=null,jHead=null,oHead=null;
        int i=1;
        while(head!=null){
            if(i%2==0){
                if(o == null){
                    o = head;
                    oHead = o;
                } else {
                    o.next = head;
                    o = o.next;
                }
            } else {
                if(j == null){
                    j = head;
                    jHead = j;
                } else {
                    j.next = head;
                    j = j.next;
                }
            }
            head = head.next;
            i++;
        }
        o.next = null;
        j.next = oHead;
        return jHead;
    }
}
